package com.lucun.es_transaction.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.lucun.es_transaction.entity.Coupon;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.CouponMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.CouponService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.javassist.tools.web.BadHttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: 郭鑫
 * @catalogue: com.lucun.es_transaction.service.impl
 * @Date: 2021/6/8 9:14
 * @Version: 1.0
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    UserLogMapper userLogMapper;

    @Override
    public List<Coupon> findAll() {
        return couponMapper.findAll();
    }

    @Override
    public JsonResult<?> insert(Coupon coupon, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(coupon)){
            throw new BaseException("没有获取数据");
        }
        int idToken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idToken)){
            throw new BaseException("解析token失败");
        }
        String name = coupon.getName();
        if (StringUtil.isEmpty(name)){
            throw new BaseException("没有获取name值");
        }
        BigDecimal deno = coupon.getDeno();
        if (ObjectUtils.isEmpty(deno)){
            throw new BaseException("没有获取deno值");
        }
        BigDecimal requirement = coupon.getRequirement();
        if (ObjectUtils.isEmpty(requirement)){
            throw new BaseException("没有获取requirement值");
        }
        Date startTime = coupon.getStartTime();
        if (ObjectUtils.isEmpty(startTime)){
            throw new BaseException("没有获取startTime");
        }
        Date endTime = coupon.getEndTime();
        if (ObjectUtils.isEmpty(endTime)){
            throw new BaseException("没有获取endTime");
        }
        int maxNum = coupon.getMaxNum();
        if (ObjectUtils.isEmpty(maxNum)){
            throw new BaseException("没有获取maxNum");
        }
        coupon.setCreateTime(new Date());
        int i = couponMapper.insert(coupon);
        UserLog userLog = new UserLog();
        userLog.setLogType((byte)1);
        userLog.setUserId(idToken);
        userLog.setCreateTime(new Date());
        if (i>0){
            userLog.setLogName(idToken+"添加优惠券成功");
            userLog.setLogStatus((byte)1);
            userLogMapper.insert(userLog);
            return new JsonResult("200","添加成功",coupon);
        }else {
            userLog.setLogName(idToken+"添加优惠券失败");
            userLog.setLogStatus((byte)0);
            userLogMapper.insert(userLog);
            return new JsonResult<>("500","添加失败",coupon);
        }
    }
}
/*   测试 */